package com.itheima.dianming.controller;

import cn.hutool.core.util.NumberUtil;
import com.itheima.dianming.pojo.domain.Stu;
import com.itheima.dianming.pojo.dto.StuDto;
import com.itheima.dianming.filter.UserHolder;
import com.itheima.dianming.service.StuService;
import com.itheima.dianming.util.DateUtils;
import com.itheima.dianming.pojo.vo.R;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/stu")
public class StuController {

    @Autowired
    private StuService stuService;

    /**
     * 查询所有的学生信息
     * @return R
     */
    @GetMapping("/list")
    public R findAll(){
        List<Stu> stus = stuService.list(null);
        return R.data(stus);
    }

    /**
     * 查询当前登陆人信息
     * @return R
     */
    @GetMapping
    public R getById(){
        Long stuId = UserHolder.getStuId();
        Stu stu = stuService.getById(stuId);
        stu.setPassword(null);
        return R.data(stu);
    }

    /**
     * 编辑学生个人信息
     * @param stu 学生信息
     * @return R
     */
    @PutMapping
    public R edit(@RequestBody Stu stu){
        Long stuId = UserHolder.getStuId();
        stu.setId(stuId);
        if(NumberUtil.isNumber(stu.getGraduateTime())){
            Integer year = DateUtils.getYear(new Date());
            stu.setWorkTime(year - Integer.parseInt(stu.getGraduateTime()));
        }
        boolean b = stuService.updateById(stu);
        return R.onflag(b);
    }


    @GetMapping("/statics")
    public R staticsStu(){
        Map result = stuService.statics();
        return R.data(result);
    }

}
